package com.message;

import com.google.protobuf.GeneratedMessageV3;
import com.google.protobuf.MessageLite;
import com.server.ServerCore;

/**
 * 消息包
 * 
 * @author zhoufang
 *
 *         2018年1月9日 下午5:02:32
 */
public class Message {

	/** 行为Id */
	private int actionId;
	/** 回调Id */
	private int responseId;
	/** 消息体 */
	private Object body;
 
	private Message() {
	}

	public static Message newMessage() {
		return new Message();
	}

	/**
	 * 创建新消息体
	 * 
	 * @param actionId
	 *            行为Id
	 * @param responseId
	 *            回调Id
	 * @param body
	 *            protobuf消息体
	 * @return
	 */
	public static Message createMessage(int actionId, int responseId, GeneratedMessageV3 body) {
		Message msg = newMessage();
		msg.setActionId(actionId);
		msg.setResponseId(responseId);
		msg.setBody(body);
		return msg;
	}
	
	/**
	 * 解析byte数组,构建新消息体
	 * @param actionId
	 * @param responseId
	 * @param bytes
	 * @return
	 */
	public static Message buildMessage(int actionId, int responseId, byte[] bytes){
		GeneratedMessageV3 body = ServerCore.getMessageManager().parseByte(actionId, bytes);
		return createMessage(actionId,responseId,body);
	}
	
	
	
	public int getActionId() {
		return actionId;
	}

	public void setActionId(int actionId) {
		this.actionId = actionId;
	}

	public int getResponseId() {
		return responseId;
	}

	public void setResponseId(int responseId) {
		this.responseId = responseId;
	}

	@SuppressWarnings("unchecked")
	public <T> T getBody() {
		return (T) body;
	}

	public void setBody(Object body) {
		this.body = body;
	}
	
}
